<script setup lang="ts">
import { onMounted, ref } from "vue";
import { useRouter } from "vue-router";
import { getProducePlanListApi } from "@/api/Produce";
import { ElMessage } from "element-plus";
import { postProducePlanAddApi } from "@/api/Produce";
// import { ProduceplanAddType } from "@/api/Produce/params";

// mockData
let sourceData = ref(["销售订单", "库存备货", "其他"]);
let stateData = ref(["未分解", "已分解", "已完成", "已关闭", "已撤回"]);
let tableData = ref([{}]);
let currentPage = ref(1);
let pageSize = ref(10);
let total = ref(0);
let showDatePicker = ref(false);
let loading = ref(false);
const $router = useRouter();

const handler = (page) => {
  currentPage.value = page;
};
const sizeHandler = (page_size) => {
  pageSize.value = page_size;
};
const gotoAdd = () => {
  $router.push("/production/addProject");
};
const handleClick = () => {};

onMounted(async () => {
  try {
    loading.value = true;
    const res = await getProducePlanListApi();
    console.log(res);
    tableData.value = res.results;
    total.value = res.results.length;
  } catch (err: any) {
    console.log(err);
    ElMessage.error("请求出错");
  } finally {
    loading.value = false;
  }
});
</script>

<template>
  <div style="padding: 20px">
    <el-card>
      <div class="operateContainer">
        <div
          class="formContainer"
          :style="showDatePicker ? 'height:85px' : 'height:32px'"
        >
          <el-form inline>
            <el-form-item>
              <el-input
                placeholder="请输入计划编号/名称"
                style="width: 250px"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-select placeholder="请选择订单来源" style="width: 250px">
                <el-option
                  v-for="item in sourceData"
                  :key="item"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-input
                placeholder="请输入产品名称"
                style="width: 250px"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-select placeholder="请选择状态" style="width: 180px">
                <el-option
                  v-for="item in stateData"
                  :key="item"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
            <div style="margin-top: 20px">
              <el-form-item>
                <el-date-picker
                  style="width: 250px"
                  type="date"
                  placeholder="请选择计划开始时间"
                ></el-date-picker>
              </el-form-item>
              <el-form-item>
                <el-date-picker
                  style="width: 250px"
                  type="date"
                  placeholder="请选择计划结束时间"
                ></el-date-picker>
              </el-form-item>
              <el-form-item>
                <el-date-picker
                  style="width: 250px"
                  type="date"
                  placeholder="请选择需求日期"
                ></el-date-picker>
              </el-form-item>
            </div>
          </el-form>
        </div>
        <div class="searchOperate">
          <div class="button">
            <el-button type="primary" plain style="width: 80px">重置</el-button>
            <el-button type="primary" style="width: 80px">查询</el-button>
          </div>
          <div class="more">
            <el-button
              type="primary"
              link
              @click="showDatePicker = !showDatePicker"
              >更多</el-button
            >
            <el-icon color="#1B9AEE" :class="{ 'rotate-180': showDatePicker }">
              <CaretBottom />
            </el-icon>
          </div>
        </div>
      </div>
    </el-card>
    <el-card style="margin-top: 20px">
      <template #header>
        <span style="font-weight: bold">生产计划</span>
      </template>
      <div class="operate">
        <el-button style="width: 80px" type="primary" @click="gotoAdd"
          >新增</el-button
        >
        <el-button style="width: 80px" type="primary" plain>编辑</el-button>
        <el-button style="width: 80px" type="primary" plain>删除</el-button>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        style="margin-top: 15px"
        border
      >
        <el-table-column
          align="center"
          type="selection"
          fixed="left"
        ></el-table-column>
        <el-table-column
          align="center"
          label="序号"
          type="index"
          width="60"
          fixed="left"
        ></el-table-column>
        <el-table-column
          align="center"
          label="计划编号"
          width="100"
          fixed="left"
          prop="planId"
        ></el-table-column>
        <el-table-column
          align="center"
          label="计划名称"
          width="90"
          fixed="left"
          prop="planName"
        ></el-table-column>
        <el-table-column
          align="center"
          label="工单数量"
          width="90"
          prop="workNumber"
        ></el-table-column>
        <el-table-column
          align="center"
          label="来源类型"
          width="90"
          prop="sourceType"
        ></el-table-column>
        <el-table-column
          align="center"
          label="成品名称"
          width="90"
          prop="productName"
        ></el-table-column>
        <el-table-column
          align="center"
          label="成品编号"
          width="90"
          prop="productId"
        ></el-table-column>
        <el-table-column
          align="center"
          label="规格型号"
          width="90"
          prop="specs"
        ></el-table-column>
        <el-table-column
          align="center"
          label="单位"
          prop="unit"
        ></el-table-column>
        <el-table-column
          align="center"
          label="计划数量"
          width="90"
          prop="planNumber"
        ></el-table-column>
        <el-table-column
          align="center"
          label="计划开工时间"
          width="110"
          prop="beginTime"
        ></el-table-column>
        <el-table-column
          align="center"
          label="计划完工时间"
          width="110"
          prop="endTime"
        ></el-table-column>
        <el-table-column
          align="center"
          label="需求日期"
          width="110"
          prop="needTime"
        ></el-table-column>
        <el-table-column
          align="center"
          label="状态"
          prop="state"
        ></el-table-column>
        <el-table-column align="center" label="操作" width="200" fixed="right">
          <template #default>
            <el-button link type="primary" @click="handleClick">
              分解
            </el-button>
            <el-button link type="primary">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30, 40]"
        :background="true"
        layout="prev,pager,next,jumper,->,sizes,total"
        :total="total"
        @current-change="handler"
        @size-change="sizeHandler"
        style="margin-top: 10px"
      ></el-pagination>
    </el-card>
  </div>
</template>

<style scoped lang="scss">
.operateContainer {
  display: flex;
  align-content: center;
  justify-content: space-between;
  .formContainer {
    width: 1060px;
    height: 32px;
    transition: height 0.3s ease; // 添加高度变化的过渡效果
    overflow: hidden; // 防止内容溢出
    .el-form-item {
      margin-bottom: 0;
    }
  }
  .searchOperate {
    display: flex;
    align-items: center;
    .more {
      margin-left: 20px;
      display: flex;
      align-items: center;
    }
  }
}

/* 定义旋转动画 */
.rotate-180 {
  transition: transform 0.3s ease;
  transform: rotate(180deg);
}
</style>
